package com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.circles.discover.DiscoverCircleActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.TimelineFragment;
import com.banciyuan.bcywebview.biz.main.mineinfo.mark.CurrentCircleSettingActivity;
import com.banciyuan.bcywebview.biz.main.mineinfo.mark.MarkCircleActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.CircleEnterStatistics;

import static com.banciyuan.bcywebview.utils.string.StringUtil.getString;

/**
 * Created by squall on 2017/7/17.
 */

public class LocalCircleHolder extends BaseView {

    private LinearLayout ll_container;

    public LocalCircleHolder(View itemView) {
        super(itemView);
        ll_container = (LinearLayout) itemView.findViewById(R.id.local_circles);
    }

    public void renderData(Context mContext) {
        readCircleStatistics(mContext);
    }

    private void readCircleStatistics(final Context mContext) {
        new AsyncTask<Void, List<CircleEnterStatistics>, List<CircleEnterStatistics>>() {
            @Override
            protected List<CircleEnterStatistics> doInBackground(Void... params) {
                Gson gson = new Gson();
                String relakey = SPConstant.CIRCLESTATISTICS + UserDataHelper.getInstance(mContext).getUid();
                String data = SPHelper.getString(mContext, SPConstant.SPNAME_CIRCLESTATISTICS, relakey, "[]");
                List<CircleEnterStatistics> clist = gson.fromJson(data, new TypeToken<List<CircleEnterStatistics>>() {
                }.getType());
                Map<String, CircleEnterStatistics> map = new HashMap<>();
                for (CircleEnterStatistics c : clist) {
                    String key = c.getType() + "#" + c.getId();
                    if (map.get(key) != null) {
                        CircleEnterStatistics temp = map.get(key);
                        temp.setCount(temp.getCount() + 1);
                        temp.setEnterTime(c.getEnterTime());
                        temp.setCover(c.getCover());
                    } else {
                        CircleEnterStatistics temp = new CircleEnterStatistics(c.getName(), c.getId(), c.getEnterTime(), c.getType(), c.getCover());
                        temp.setCount(1);
                        map.put(key, temp);
                    }
                }
                List<CircleEnterStatistics> sort = new ArrayList<>();
                for (String s : map.keySet()) {
                    sort.add(map.get(s));
                }
                Collections.sort(sort, new Comparator<CircleEnterStatistics>() {
                    @Override
                    public int compare(CircleEnterStatistics lhs, CircleEnterStatistics rhs) {
                        return lhs.compareTo(rhs);
                    }
                });
                if (sort.size() >= 8) {
                    return sort.subList(0, 8);
                } else {
                    return sort;
                }
            }

            @Override
            protected void onPostExecute(List<CircleEnterStatistics> circleEnterStatisticses) {
                ll_container.removeAllViews();
                int index = circleEnterStatisticses.size() > 0 ? circleEnterStatisticses.size() + 2 : 3;
                for (int i = 0; i < index; i++) {
                    View viewroot = View.inflate(mContext, R.layout.timeline_local_circle_item, null);
                    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    ll.setMargins(UIUtils.dip2px(8, mContext), 0, UIUtils.dip2px(8, mContext), 0);
                    viewroot.setLayoutParams(ll);
                    ImageView iv = (ImageView) viewroot.findViewById(R.id.iv_work);
                    TextView tv = (TextView) viewroot.findViewById(R.id.tv_work);
                    if (i < circleEnterStatisticses.size()) {
                        final CircleEnterStatistics c = circleEnterStatisticses.get(i);
                        ImageLoader.getInstance().displayImage(c.getCover(), iv, ImageOptions.options);
                        if (!TextUtils.isEmpty(c.getName())) {
                            tv.setText(c.getName());
                        }
                        viewroot.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (c.getType().equals("work")) {
                                    Intent intent = new Intent();
                                    intent.putExtra(gotoUtil.INTENT_VALUE_TWO, c.getId());
                                    intent.putExtra("from", 1);
                                    intent.setClass(mContext, CircleSmoothActivity.class);
                                    mContext.startActivity(intent);
                                } else if (c.getType().equals("tag")) {
                                    Intent intent = new Intent();
                                    intent.putExtra(gotoUtil.INTENT_VALUE_ONE, c.getName());
                                    intent.putExtra("from", 1);
                                    intent.setClass(mContext, CircleTagSmoothActivity.class);
                                    mContext.startActivity(intent);
                                }
                            }
                        });
                    } else if (i == circleEnterStatisticses.size()) {
                        iv.setImageResource(R.drawable.i_foucs);
                        tv.setText(mContext.getString(R.string.i_focus));
                        viewroot.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (!TextUtils.isEmpty(UserDataHelper.getInstance(mContext).getToken())) {
                                    Intent intent = new Intent(mContext, MarkCircleActivity.class);
                                    intent.putExtra(gotoUtil.INTENT_VALUE_ONE, MarkCircleActivity.TYPE_WORK);
                                    intent.putExtra(gotoUtil.INTENT_VALUE_TWO, UserDataHelper.getInstance(mContext).getUid());
                                    mContext.startActivity(intent);
                                } else {
                                    MyToast.show(mContext, getString(R.string.login_first));
                                }
                            }
                        });
                    } else if (i == circleEnterStatisticses.size() + 1) {
                        if (circleEnterStatisticses.size() > 0) {
                            iv.setImageResource(R.drawable.i_setting);
                            tv.setText(mContext.getString(R.string.i_setting));
                            viewroot.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(mContext, CurrentCircleSettingActivity.class);
                                    ((Activity) mContext).startActivityForResult(intent, TimelineFragment.CIRCLE_SETTING);
                                }
                            });
                        } else {
                            iv.setImageResource(R.drawable.i_hotcircle);
                            tv.setText(mContext.getString(R.string.work_circle));
                            viewroot.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    gotoUtil.gotoAct(mContext, DiscoverCircleActivity.class, DiscoverCircleActivity.TYPE_WORK);

                                }
                            });
                        }
                    } else if (i == circleEnterStatisticses.size() + 2) {
                        iv.setImageResource(R.drawable.i_interest);
                        tv.setText(mContext.getString(R.string.tag_circle));
                        viewroot.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                gotoUtil.gotoAct(mContext, DiscoverCircleActivity.class, DiscoverCircleActivity.TYPE_TAG);
                            }
                        });
                    }
                    ll_container.addView(viewroot);
                }
            }
        }.execute();
    }

}
